import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import "../settings"
import "../components"

Window {

    id: root

    width: 1240*dp
    height: 450*dp
    title: "切换设备"


    modality: Qt.WindowModal
    flags: Qt.Window | Qt.WindowTitleHint |Qt.WindowSystemMenuHint | Qt.WindowCloseButtonHint

    property string selectedDevice: ""
    property var equList: []

    signal accept(var equId)
    signal reject()

    onVisibleChanged: {
        if(visible){
            refresh()
        }
    }

    function refresh(){
        equList = CSetUp.queryEquInfo()
        var i = 0;
        var list = layout_device.children
        for(let key in equList){
            list[i].equId =  equList[key]["Id"]
            list[i].name =   equList[key]["EquName"]
            list[i].type =   equList[key]["EquConnectType"]===0 ? 0 : 1
            list[i].route =  equList[key]["EquConnectType"]===0 ? equList[key]["Ip"] : equList[key]["SerialPort"]
            list[i].status = equList[key]["EquStatus"]
            list[i].visible = true
            i ++
        }
        for(; i < list.length; i ++){
            list[i].visible = false
        }
//        console.log("当前设备数量: " + equList.length)
    }

    PushButton{
        height: 24*dp
        width: 68*dp
        radius: 12*dp
        title: "刷新"
        button_color: "#F3F3F3"
        text_color: "#1677FF"
        y: 20*dp
        x: 28*dp
        Timer{
            id: timer_hide
            repeat: false
            interval: 20
            onTriggered: {
                layout_device.visible = true
            }
        }onReleased: {
            layout_device.visible = false
            refresh()
            timer_hide.start()
        }
    }

    ScrollView{
        height: 300*dp
        width: 1200*dp
        x: 28*dp
        y: 60*dp
        clip: true
        Grid{
            id: layout_device
            columns: 4
            spacing: 20*dp
            DeviceItem{  // 133*280
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }DeviceItem{
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }DeviceItem{
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }DeviceItem{
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }DeviceItem{
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }DeviceItem{
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }DeviceItem{
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }DeviceItem{
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }DeviceItem{
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }DeviceItem{
                isOperable: false
                isSelected: selectedDevice === equId
                onReleased: {
                    selectedDevice = equId
                }
            }Rectangle{
                height: 1
                width: 1
            }
        }
    }

    PushButton{
        y: 380*dp
        x: 490*dp
        title: "取消"
        defaultMode2: true
        onReleased: {
            root.reject()
            root.close()
        }
    }
    PushButton{
        x: 630*dp
        y: 380*dp
        title: "确定"
        defaultMode1: true
        onReleased: {
            var equId = equList[selectedDevice]["Id"]
            root.accept(equId)
            root.close()
        }
    }

}
